import { _decorator, Button, color, find, instantiate, Label, NodePool, ProgressBar, tween, Tween, UITransform, Vec3 } from 'cc';
import { BaseUI } from '../../../../frame/component/ui.base';
import GameDC from '../../../data/data.game.center';
import ConfManager from '../../../manager/config.manager';
import DataManager from '../../../manager/data.manager';
import { Float } from '../float';
import { Popup } from '../pop-up';
const { ccclass, property } = _decorator;

@ccclass('UIHomeTop')
export class UIHomeTop extends BaseUI {
	private uitrans: UITransform;
	private vecasit: mtec.cc.VecAssist;
	private icon_diamond: UITransform;
	private bar_level: ProgressBar;
	private lbl_diamond: Label;
	private lbl_level: Label;

	private lbl_diamond_pool: NodePool;

	private ui_status: 'enter' | 'leave';

	protected initAfterOnLoad(){
		this.init();
	}

	protected start(){
		this.adaptUI();
		this.node.active = false;
	}

	private init(){
		this.uitrans = this.node.getComponent(UITransform);
		this.vecasit = new mtec.cc.VecAssist(this.node);

		this.icon_diamond = find('diamond/icon', this.node).getComponent(UITransform);
		this.lbl_diamond = this.icon_diamond.node.parent.getChildByName('lbl-value').getComponent(Label);
		this.lbl_diamond_pool = new NodePool();
		let node_level = this.node.getChildByName('level');
		this.bar_level = node_level.getChildByName('bar').getComponent(ProgressBar);
		this.lbl_level = node_level.getChildByName('lbl-level').getComponent(Label);
	}

	private adaptUI(){
		this.vecasit.getPosition('center', 'over_top');
		this.node.setPosition(this.vecasit.vec);

		let background = this.node.getChildByName('background');
		let va_bg = new mtec.cc.VecAssist(background);
		va_bg.vec.add3f(0, -138, 0);
		background.setPosition(va_bg.vec);
	}

	protected _click_event_: { [name: string]: (this: UIHomeTop, button: Button) => void; } = {
		level(btn){
			if(GameDC.Status.home_ui_btn_lock){
				return void 0;
			}

			GameDC.Trigger.UIAchieve = true;
		},
		diamond(){
			if(GameDC.Status.home_ui_btn_lock){
				return void 0;
			}
			GameDC.Status.home_ui_btn_lock = true;

			Popup.Diamond().then(()=>GameDC.Status.home_ui_btn_lock = false);
		}
	}

	protected listening(){

		let diamond_obj = {value: 0};

		((o: any, lbl: Label)=>{
			let value: number;
			Reflect.deleteProperty(o, 'value');
			Reflect.defineProperty(o, 'value', {
				get(){
					return value;
				},
				set(v){
					value = v;
					lbl.string = Math.round(v).toString();
				}
			});
		})(diamond_obj, this.lbl_diamond);
		diamond_obj.value = GameDC.User.diamond;


		let list = [
			this.follow(GameDC.Trigger, 'UIHome', (o, v)=>this.onUIHomeTrigger(o, v), false),
			this.follow(GameDC.Trigger, 'FLOAT_DIAMOND', (o, v)=>this.onFloatDiamondTrigger(v), false),
			this.follow(GameDC.User, 'diamond', (o, v)=>{
				Tween.stopAllByTarget(diamond_obj);
				tween(diamond_obj).to(0.8, {value: v}).start();
			}, false),
			this.follow(GameDC.User, 'master_free_level', ()=>this.refreshMasterLevel()),
			this.follow(GameDC.Cache, 'remove_count', ()=>this.refreshMasterLevel())
		];
		return list;
	}

	private enter(){
		if(this.ui_status==='enter'){
			return void 0;
		}
		this.ui_status = 'enter';

		this.node.active = true;

		this.vecasit.getPosition('center', 'top');
		if(mtec.cc.is_long_screen){
			this.vecasit.vec.y -= 100;
		}

		Tween.stopAllByTarget(this.node);
		tween(this.node)
		.to(0.5, {position: this.vecasit.vec}, {easing: 'cubicOut'})
		.start();

		if(GameDC.Cache.round_curr>=4){
			GameDC.Trigger.UILuckyBox = true;
		}
	}

	private leave(){
		if(this.ui_status==='leave'){
			return void 0;
		}
		this.ui_status = 'leave';

		this.vecasit.getPosition('center', 'over_top');

		Tween.stopAllByTarget(this.node);
		tween(this.node)
		.to(0.5, {position: this.vecasit.vec}, {easing: 'cubicOut'})
		.call(()=>{
			this.node.active=false;
		}).start();

		if(GameDC.Cache.round_curr>=4){
			GameDC.Trigger.UILuckyBox = false;
		}
	}

	private onUIHomeTrigger(ov: boolean, show: boolean){
		if(show){
			this.enter();
		}else{
			this.leave();
		}
	}

	private lbl_diamond_color_dr = color(99, 243, 44);
	private lbl_diamond_color_dd = color(251, 66, 69);
	private lbl_diamond_offset = new Vec3(0, 40, 0);
	private async onFloatDiamondTrigger(num: number){
		let vec = DataManager.diamond_float_from;
		if(num>0 && vec){
			await Float.Reap(this.icon_diamond.node, mtec.number.random(6, 8), vec);
		}

		let lbl = (this.lbl_diamond_pool.get() ?? instantiate(this.lbl_diamond.node)).getComponent(Label);
		lbl.color = num>0 ? this.lbl_diamond_color_dr : this.lbl_diamond_color_dd;
		lbl.string = num>0 ? `+${num}` : `${num}`;
		lbl.node.setScale(0.8, 0.8, 1);
		lbl.node.active = true;
		lbl.node.setParent(this.lbl_diamond.node.parent);
		lbl.node.setPosition(this.lbl_diamond.node.position);

		tween(lbl.node).by(0.6, {position: this.lbl_diamond_offset}).delay(0.6)
		.call(()=>this.lbl_diamond_pool.put(lbl.node)).start();
	}

	private async refreshMasterLevel(){
		await Promise.all([DataManager.UserInfoSynced, DataManager.ConfigInited]);
		let next_idx = ConfManager.masterList.findIndex(el=>el.level > GameDC.User.master_free_level);
		if(next_idx < 0){
			next_idx = ConfManager.masterList.length;
		}
		let next_data = ConfManager.masterList[next_idx];

		let remove_count = next_data.eliminateCount + Math.min(0, DataManager.master_remove - next_data.remove_total);
		this.bar_level.progress = remove_count / next_data.eliminateCount;
		this.lbl_level.string = 'Lv.' + next_data.level;
	}
}

